package com.qiguliuxing.dts.db.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiguliuxing.dts.db.dao.DtsTopicMapper;
import com.qiguliuxing.dts.db.domain.DtsTopic;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DtsTopicService {
	@Resource
	private DtsTopicMapper topicMapper;

	public List<DtsTopic> queryList(int offset, int limit) {
		return queryList(offset, limit, "add_time", "desc");
	}

	public List<DtsTopic> queryList(int offset, int limit, String sort, String order) {
		QueryWrapper<DtsTopic> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsTopic::getDeleted, false);
		queryWrapper.orderByDesc(sort);
		IPage<DtsTopic> iPage = new Page<>(offset, limit);
		return topicMapper.selectPage(iPage, queryWrapper).getRecords();
	}

	public int queryTotal() {
		QueryWrapper<DtsTopic> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsTopic::getDeleted, false);
		return topicMapper.selectCount(queryWrapper).intValue();
	}

	public DtsTopic findById(Integer id) {
		QueryWrapper<DtsTopic> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsTopic::getId, id);
		queryWrapper.lambda().eq(DtsTopic::getDeleted, false);
		return topicMapper.selectOne(queryWrapper);
	}

	public List<DtsTopic> queryRelatedList(Integer id, int offset, int limit) {
		QueryWrapper<DtsTopic> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsTopic::getId, id);
		queryWrapper.lambda().eq(DtsTopic::getDeleted, false);
		List<DtsTopic> topics = topicMapper.selectList(queryWrapper);
		if (topics.size() == 0) {
			return queryList(offset, limit, "add_time", "desc");
		}
		DtsTopic topic = topics.get(0);
		queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().ne(DtsTopic::getId, topic.getId());
		queryWrapper.lambda().eq(DtsTopic::getDeleted, false);
		IPage<DtsTopic> iPage = new Page<>(offset, limit);
		List<DtsTopic> relateds =  topicMapper.selectPage(iPage, queryWrapper).getRecords();
		if (relateds.size() != 0) {
			return relateds;
		}

		return queryList(offset, limit, "add_time", "desc");
	}

	public List<DtsTopic> querySelective(String title, String subtitle, Integer page, Integer limit, String sort,
			String order) {
		QueryWrapper<DtsTopic> queryWrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(title)) {
			queryWrapper.lambda().like(DtsTopic::getTitle, title);
		}
		if (StrUtil.isNotEmpty(subtitle)) {
			queryWrapper.lambda().like(DtsTopic::getSubtitle, subtitle);
		}
		queryWrapper.lambda().eq(DtsTopic::getDeleted, false);
		if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)) {
			queryWrapper.orderByDesc(sort);
		}
		IPage<DtsTopic> iPage = new Page<>(page, limit);
		return topicMapper.selectPage(iPage, queryWrapper).getRecords();
	}

	public int updateById(DtsTopic topic) {
		topic.setUpdateTime(LocalDateTime.now());
		UpdateWrapper<DtsTopic> updateWrapper = new UpdateWrapper<>();
		updateWrapper.lambda().eq(DtsTopic::getId, topic.getId());
		return topicMapper.update(topic, updateWrapper);
	}

	public void deleteById(Integer id) {
		topicMapper.deleteById(id);
	}

	public void add(DtsTopic topic) {
		topic.setAddTime(LocalDateTime.now());
		topic.setUpdateTime(LocalDateTime.now());
		topicMapper.insert(topic);
	}

}
